package com.ctz.commonframe.ui.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ctz.commonframe.R;
import com.ctz.commonframe.entity.ImageItem;
import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.utils.GlideApp;

import java.util.ArrayList;
import java.util.List;

public class ImageDisplayActivity extends TitleBarActivity {
	private ViewPager viewPager;

	private List<ImageItem> imageItemList;
	private int currentPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_display);

		Intent intent = getIntent();
		imageItemList = (List<ImageItem>) intent.getSerializableExtra("datas");
		currentPosition = intent.getIntExtra("index", 0);

		viewPager = (ViewPager) findViewById(R.id.vp_image);

		setTitleMiddleText((currentPosition + 1) + "/" + imageItemList.size());
		setBackgroundColor(Color.BLACK);
		viewPager.setAdapter(new ImageViewPagerAdapter(this, imageItemList));
		viewPager.setCurrentItem(currentPosition);
		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				setTitleMiddleText((position + 1) + "/" + imageItemList.size());
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	private class ImageViewPagerAdapter extends PagerAdapter {
		private List<ImageItem> imageItemList;
		private List<ImageView> imageViewList;

		public ImageViewPagerAdapter(Context context, List<ImageItem> imageItemList) {
			super();
			this.imageItemList = imageItemList;
			imageViewList = new ArrayList<>(imageItemList.size());
			for(int i = 0; i < imageItemList.size(); i++){
				ImageView imageView = new ImageView(context);
				imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
				imageView.setBackgroundColor(Color.DKGRAY);
				GlideApp.with(context).clear(imageView);
				GlideApp.with(context).load(imageItemList.get(i).imagePath).fitCenter().into(imageView);
				imageViewList.add(imageView);
			}
		}

		@Override
		public int getCount() {
			if (imageItemList != null) {
				return imageItemList.size();
			}
			return 0;
		}

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager)container).addView(imageViewList.get(position));
			return imageViewList.get(position);
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(imageViewList.get(position));
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return (view == object);
		}
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dipToPx(Context context, float dpValue) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics());
	}
}
